import { defineStore } from "pinia";
import { login } from "@/api/SysUserApi.ts";

interface UserInfo {
    id: number | null;
    account: string;
    realName: string;
}

export const useUserStore = defineStore('user', {
    state: (): { userInfo: UserInfo } => ({
        userInfo: {
            id: null,
            account: '',
            realName: ''
        }
    }),
    getters: {
        getUserInfo: (state): UserInfo => {
            return state.userInfo;
        }
    },
    actions: {
        async login(account: string, password: string): Promise<void> {
            try {
                const response = await login(account, password);
                if (response && typeof response === 'object' && 'id' in response && 'account' in response && 'realName' in response) {
                    this.userInfo = response;
                } else {
                    console.error("Invalid response format from login API");
                }
            } catch (error) {
                console.error("Login failed:", error);
                // 可以在这里触发一个全局的错误通知机制，告知用户登录失败
            }
        }
    }
});
